package com.k2data.k2app.util.schedule;

import com.k2data.k2app.common.MyConstant;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

/**
 * Created by clb on 17-7-17.
 */
@Service
public class ScheduleJobService {
    @Autowired
    SchedulerFactoryBean schedulerFactoryBean;

    @Autowired
    private MyConstant myConstant;
    /**
     * 启动所有的定时任务
     * @throws Exception
     */
    public void getAllSchedules() throws Exception {
        // 定时刷新设备状态的定时器
        addDeviceStatusShedule();
    }

    public  void addDeviceStatusShedule() throws Exception{
        String cron = String.format("0 */%s * * * ? *",myConstant.getCron());
        ScheduleJob scheduleJob = new ScheduleJob();
        scheduleJob.setJobDesc("定时查看设备是否停机");
        scheduleJob.setJobName("acceptData");
        scheduleJob.setJobGroup("K2ASSET_MONITOR");
        scheduleJob.setCronExpression(cron);
        addScheduleDeviceStatus(scheduleJob);
    }
    /**
     * @Description: 添加一个定时任务,定时判断设备是否停机
     * @param job
     * @throws SchedulerException
     */

    public void addScheduleDeviceStatus(ScheduleJob job) throws SchedulerException {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        TriggerKey triggerKey = TriggerKey.triggerKey(job.getJobName(), job.getJobGroup());
        //获取trigger，即在spring配置文件中定义的 bean id="myTrigger"
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        //不存在，创建一个
        if (null == trigger) {
            JobDetail jobDetail = JobBuilder.newJob(QuartzJobAcceptDataFactory.class)
                    .withIdentity(job.getJobName(), job.getJobGroup()).build();
            jobDetail.getJobDataMap().put("scheduleJobAcceptData", job);
            //表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
                    .getCronExpression()).withMisfireHandlingInstructionFireAndProceed();
            //按新的cronExpression表达式构建一个新的trigger
            trigger = TriggerBuilder.newTrigger().startNow().withIdentity(job.getJobName(), job.getJobGroup()).withSchedule(scheduleBuilder).build();
            scheduler.scheduleJob(jobDetail, trigger);
        } else {
            // Trigger已存在，那么更新相应的定时设置
            //表达式调度构建器
            CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(job
                    .getCronExpression()).withMisfireHandlingInstructionFireAndProceed();
            //按新的cronExpression表达式重新构建trigger
            trigger = trigger.getTriggerBuilder().startNow().withIdentity(triggerKey)
                    .withSchedule(scheduleBuilder).build();
            //按新的trigger重新设置job执行
            scheduler.rescheduleJob(triggerKey, trigger);
        }
        System.out.println("设备状态定时触发器执行完毕！");
        // 启动
        if (!scheduler.isShutdown()) {
            scheduler.start();
        }
    }
}
